using System;
using System.Collections.Generic;
using System.Text;

namespace Snooze.Model
{
    using Castle.ActiveRecord;
    using NHibernate.Expression;
    using System.ComponentModel;
    using System.Reflection;
    using System.Collections;
    using System.Xml;
    using System.Xml.Serialization;

    [ActiveRecord]
    public abstract class Persistent : ActiveRecordBase, IXmlSerializable
    {
        #region members
        private int id;
        #endregion

        #region public persistent properties
        [PrimaryKey]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        #endregion

        public string Xml
        {
            get
            {
				return GetXml(Serializer.MAX_DEPTH);
            }
            set
            {
                SetXml(value, Serializer.MAX_DEPTH);
            }
        }

        public string GetXml(int depth)
        {
            return Serializer.Instance.Serialize(this, null, depth);
        }

        public void SetXml(string xml, int depth)
        {
            Serializer.Instance.Deserialize(this, xml, depth);
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
			// On mono it is realy important to return null here
			// or otherwise the XMLSerializer will simply hang
			return null;
        }

        public void ReadXml(XmlReader reader)
        {
            Serializer.Instance.ReadXml(reader);
        }

        public void WriteXml(XmlWriter writer)
        {
            Serializer.Instance.WriteXml(writer, this);
        }

        #endregion
    }
}
